﻿#include "CameraCommon/CameraLoader.h"

namespace Qly {
CameraLoader::CameraLoader(QString dllFullName, QObject *parent)
    : QObject{parent}
{
    qDebug() << "in CameraLoader::CameraLoader";
    pCameraLib = new QLibrary(this);
    pCameraLib->setFileName(dllFullName);

    qDebug() << "in CameraLoader::CameraLoader resolve";
    CreateInstance = (CreateInstanceType) pCameraLib->resolve("CreateInstance");
    Release = (ReleaseType) pCameraLib->resolve("Release");

    qDebug() << "CreateInstance = " << CreateInstance;
    if(CreateInstance)
    {
        qDebug() << "CreateInstance";
        m_pInterface = CreateInstance(this);
    }
}

CameraLoader::~CameraLoader()
{
    if(m_pInterface && Release)
    {
        Release(m_pInterface);
    }
    pCameraLib->unload();
}

bool CameraLoader::isValid()
{
    return (Release && CreateInstance);
}

CameraFactoryInterface *CameraLoader::defaultInstance()
{
    return m_pInterface;
}


CameraFactoryInterface *CameraLoader::createInstance(QObject * pObject)
{
    CameraFactoryInterface * p = nullptr;
    if(CreateInstance)
    {
        p = CreateInstance(pObject);
    }
    return p;
}

void CameraLoader::release(CameraFactoryInterface *p)
{
    if(Release)
    {
        Release(p);
    }
}

}

